﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Models;
using Vit.Web;
using Vit.Web.Models;

namespace Vit.Mvc.Controllers
{
    public abstract class UnityControllerBase : Controller
    {
        protected IWebSite CurrentSite
        {
            get
            {
                IWebSite site = Session["CurrentSite"] as IWebSite;
                if (site == null)
                {
                    this.Unity.PortalService.Run(p =>
                    {
                        site = p.GetWebSite(Request.RawUrl);
                        return site != null;
                    });
                    Session["CurrentSite"] = site;
                }
                return site;
            }
        }

        protected IPage CurrentPage
        {
            get
            {
                IPage pageInfo = WebUnity.Current.PortalService.Run(p => p.GetPage(Request.RawUrl), p => p != null);
                return pageInfo;
            }
        }

        protected ITheme CurrentTheme
        {
            get
            {
                return WebUnity.Current.PortalService.Run(p => p.GetCurrentTheme(), p => p != null);
            }
        }

        protected ISkin CurrentSkin
        {
            get
            {
                return WebUnity.Current.PortalService.Run(p => p.GetCurrentSkin(), p => p != null);
            }
        }

        protected EditMode CurrentEditMode
        {
            get
            {
                return WebUnity.Current.PortalService.Run(p => p.GetCurrentUserMode(), p => p != null);
            }
        }

        protected ILocale CurrentLocale
        {
            get
            {
                return CurrentUser.Locale;
            }
        }

        protected WebUnity Unity
        {
            get
            {
                return WebUnity.Current;
            }
        }

        protected IUser CurrentUser
        {
            get
            {
                return Unity.MembershipService.Run<IUser>(p => p.GetCurrentUser(), (u) => u != null);
            }
        }
    }
}
